// pages/grow2/grow2.js
var app = getApp()
var util = require("../../utils/util.js");
const moment = require("../../utils/moment.min.js")
Page({

  /**
   * 页面的初始数据
   */
  data: {
    project:'体适能',
    current: '0',
    arrayPhysicFitness: ['50米', '一分钟跳绳', '立定跳远', '一分钟仰卧起坐','坐位体前屈'],  //体适能
    arrayJumpRope: ['50米', '一分钟跳绳', '立定跳远', '一分钟仰卧起坐', '坐位体前屈'],  //花式跳绳
    arraySwimText: ['50米', '一分钟跳绳', '立定跳远', '一分钟仰卧起坐', '坐位体前屈'],  //篮球
    arrayBadminton: ['50米', '一分钟跳绳', '立定跳远', '一分钟仰卧起坐','坐位体前屈'],  //羽毛球
    // arrayJumpRope: ['30s单摇', '30s双摇', '1min棉绳', '3min单摇'],   //花式跳绳
    // arraySwimText: ['25米', '200米'],  //游泳
    // arrayBadminton: ['50米', '一分钟跳绳', '立定跳远', '一分钟仰卧起坐'],  //羽毛球
    arrayMidText: ['四分钟跳绳','800/1000米', '50米', '立定跳远', '一分钟仰卧起坐/引体向上'],  //中考体育
    index1: 0,
    index2: 0,
    index3: 0,
    index4: 0,
    index5:0,
    physicFitness:[],
    jumpRope:[],
    swimText:[],
    badminton:[],
    midText:[],
    physicFitnessTest: [],
    jumpRopeTest: [],
    swimTextTest: [],
    badmintonTest: [],
    midTextTest: [],
    childcode:'',
  }, 
  // 体适能picker
  bindPickerChangePhysicFitness: function (e) {
    console.log('picker发送选择改变，携带值为', e.detail.value)
    var physicFitness = this.data.physicFitness
    var Array1 = [];
    for (const i of physicFitness){
      console.log(i)
      var createAt = moment(i.date).format('YYYY-MM-DD')
      if (e.detail.value == 0){  //是50米测试
        let item = {
          date: createAt,
          project: i.run50m
        }
        Array1.push(item)
      } else if (e.detail.value == 1){ // 一分钟跳绳
        let item = {
          date: createAt,
          project: i.jumprope1min
        }
        Array1.push(item)
      } else if (e.detail.value == 2){ //立定跳远
        let item = {
          date: createAt,
          project: i.longjump
        }
        Array1.push(item)
      } else if (e.detail.value == 3){ //一分钟仰卧起坐
        let item = {
          date: createAt,
          project: i.situp
        }
        Array1.push(item)
      } else if (e.detail.value== 4){//坐位体前屈
        let item = {
          date: createAt,
          project: i.sitandreach
        }
        Array1.push(item)
      }
    }
    this.setData({
      index1: e.detail.value,
      physicFitnessTest: Array1
    });

  },
  // 花式跳绳picker
  bindPickerChangeJumpRope: function (e) {
    // console.log('picker发送选择改变，携带值为', e.detail.value)
    var jumpRope = this.data.jumpRope
    var Array1 = [];
    for (const i of jumpRope) {
      var createAt = moment(i.date).format('YYYY-MM-DD')
      if (e.detail.value == 0) {  //是50米测试
        let item = {
          date: createAt,
          project: i.run50m
        }
        Array1.push(item)
      } else if (e.detail.value == 1) { // 一分钟跳绳
        let item = {
          date: createAt,
          project: i.jumprope1min
        }
        Array1.push(item)
      } else if (e.detail.value == 2) { //立定跳远
        let item = {
          date: createAt,
          project: i.longjump
        }
        Array1.push(item)
      } else if (e.detail.value == 3) { //一分钟仰卧起坐
        let item = {
          date: createAt,
          project: i.situp
        }
        Array1.push(item)
      } else if (e.detail.value == 4) {//坐位体前屈
        let item = {
          date: createAt,
          project: i.sitandreach
        }
        Array1.push(item)
      }
    }
    this.setData({
      index2: e.detail.value,
      jumpRopeTest: Array1
    });
  },
  // 游泳picker
  bindPickerChangeSwimText: function (e) {
    // console.log('picker发送选择改变，携带值为', e.detail.value)
    var swimText = this.data.swimText
    var Array1 = [];
    for (const i of swimText) {
      // console.log(i)
      var createAt = moment(i.date).format('YYYY-MM-DD')
      if (e.detail.value == 0) {  //是50米测试
        let item = {
          date: createAt,
          project: i.run50m
        }
        Array1.push(item)
      } else if (e.detail.value == 1) { // 一分钟跳绳
        let item = {
          date: createAt,
          project: i.jumprope1min
        }
        Array1.push(item)
      } else if (e.detail.value == 2) { //立定跳远
        let item = {
          date: createAt,
          project: i.longjump
        }
        Array1.push(item)
      } else if (e.detail.value == 3) { //一分钟仰卧起坐
        let item = {
          date: createAt,
          project: i.situp
        }
        Array1.push(item)
      } else if (e.detail.value == 4) {//坐位体前屈
        let item = {
          date: createAt,
          project: i.sitandreach
        }
        Array1.push(item)
      }
    }
    this.setData({
      index3: e.detail.value,
      swimTextTest: Array1
    });
  },
  // 羽毛球picker
  bindPickerChangeBadminton: function (e) {
    // console.log('picker发送选择改变，携带值为', e.detail.value)
    var badminton = this.data.badminton
    var Array1 = [];
    for (const i of badminton) {
      // console.log(i)
      var createAt = moment(i.date).format('YYYY-MM-DD')
      if (e.detail.value == 0) {  //是50米测试
        let item = {
          date: createAt,
          project: i.run50m
        }
        Array1.push(item)
      } else if (e.detail.value == 1) { // 一分钟跳绳
        let item = {
          date: createAt,
          project: i.jumprope1min
        }
        Array1.push(item)
      } else if (e.detail.value == 2) { //立定跳远
        let item = {
          date: createAt,
          project: i.longjump
        }
        Array1.push(item)
      } else if (e.detail.value == 3) { //一分钟仰卧起坐
        let item = {
          date: createAt,
          project: i.situp
        }
        Array1.push(item)
      } else if (e.detail.value == 4) {//坐位体前屈
        let item = {
          date: createAt,
          project: i.sitandreach
        }
        Array1.push(item)
      }
    }
    this.setData({
      index4: e.detail.value,
      badmintonTest: Array1
    });
  },
  // 中考体育picker
  bindPickerChangeMidText: function (e) {
    // console.log('picker发送选择改变，携带值为', e.detail.value)
    var midText = this.data.midText
    var Array1 = [];
    // console.log(midText);
    for (const i of midText) {
      // console.log(i)
      var createAt = moment(i.date).format('YYYY-MM-DD')
      if (e.detail.value == 0) {  //4min跳绳
        let item = {
          date: createAt,
          project: i.jumprope4min
        }
        Array1.push(item)
      } else if (e.detail.value == 1) { // 800/1000米
        let item = {
          date: createAt,
          project: i.run800mor1000m
        }
        Array1.push(item)
      } else if (e.detail.value == 2) { //50米
        let item = {
          date: createAt,
          project: i.run50m
        }
        Array1.push(item)
      } else if (e.detail.value == 3) { //立定跳远
        let item = {
          date: createAt,
          project: i.longjump
        }
        Array1.push(item)
      } else if (e.detail.value == 4) {//一分钟仰卧起坐/引体向上
        let item = {
          date: createAt,
          project: i.pulluporsitup
        }
        Array1.push(item)
      }
    }
    this.setData({
      index5: e.detail.value,
      midTextTest: Array1
    })
    // console.log('=====dddddddddddddddddd=========')
    // console.log(this.data.midTextTest);
  },
  // tabbar切换的方法
  handleChange: function (e) {
    var me = this;
    var name = '体适能'
    var currentTab = e.detail.key;
    if (currentTab == 0){
      name = '体适能'
    } else if (currentTab == 1) {
      name = '花式跳绳'
    } else if (currentTab == 2) {
      name = '游泳'
    } else if (currentTab == 3) {
      name = '羽毛球'
    } else if (currentTab == 4) {
      name = '中考体育'
    }
    console.log(name)
    me.setData({
      current: currentTab,
      project: name
    });
    this.getTestInfo();
  },
  getTestInfo: function (e) {
    var me = this;
    // console.log('--------------------')
    // console.log(me.data.childcode)
    // console.log(me.data.project)
    wx.request({
      url: app.serverUrl + '/getGrowProcess',
      data: {
        childcode: me.data.childcode,
        classname: me.data.project,
      },
      header: {
        'content-type': 'application/x-www-form-urlencoded' // 默认值
      },
      method: "POST",
      success: function (res) {
        if (res.data.status == 200) {
          // console.log('============测试数据=======')
          // console.log(res.data.data)
          // console.log(me.data.project)
          if (me.data.project == '体适能') {
            var arr1 = [];
            for (const i of res.data.data) {
              var createAt = moment(i.date).format('YYYY-MM-DD')
              let item = {
                date: createAt,
                project: i.run50m
              }
              arr1.push(item)
            }
            me.setData({
              physicFitness: res.data.data,
              physicFitnessTest: arr1
            })
          } else if (me.data.project == '花式跳绳'){
            var arr2 = [];
            for (const i of res.data.data) {
              var createAt = moment(i.date).format('YYYY-MM-DD')
              let item = {
                date: createAt,
                project: i.run50m
              }
              arr2.push(item)
            }
            me.setData({
              jumpRope: res.data.data,
              physicFitnessTest: arr2
            })
            me.setData({
              jumpRope: res.data.data,
              jumpRopeTest:arr2
            })
          } else if (me.data.project == '游泳') {
            var arr3 = [];
            for (const i of res.data.data) {
              var createAt = moment(i.date).format('YYYY-MM-DD')
              let item = {
                date: createAt,
                project: i.run50m
              }
              arr2.push(item)
            }
            me.setData({
              swimText: res.data.data,
              swimTextTest: arr3
            })
          } else if (me.data.project == '羽毛球') {
            var arr4 = [];
            for (const i of res.data.data) {
              var createAt = moment(i.date).format('YYYY-MM-DD')
              let item = {
                date: createAt,
                project: i.run50m
              }
              arr2.push(item)
            }
            me.setData({
              badminton: res.data.data,
              badmintonTest: arr4
            })
          } else if (me.data.project == '中考体育') {
            var arr5 = [];
            for (const i of res.data.data) {
              var createAt = moment(i.date).format('YYYY-MM-DD')
              let item = {
                date: createAt,
                project: i.jumprope4min
              }
              arr5.push(item)
            }
            me.setData({
              midText: res.data.data,
              midTextTest: arr5
            })
            // console.log('66666666666666666666666')
            // console.log(me.data.midText)
          }
        } else {
          console.log(res.data.msg)
        }
      },
    })
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    var me = this;
    //判断是否绑定了孩子
    wx.getStorage({
      key: 'childcode',
      success: function (res) {
        // console.log('拿到缓存中的childcode')
        // console.log(res.data)
        var childcode = res.data;
        me.setData({
          childcode: childcode
        });
        if (childcode == '' || childcode == null || childcode == undefined) {
          wx.redirectTo({
            url: '/pages/kid/kid',
          })
        } else {
          me.getTestInfo();
        }
      }
    })


  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})